package de.geraldschenke.tictactoe.common.ai;

import android.util.Log;
import de.geraldschenke.tictactoe.common.GameEvent;
import de.geraldschenke.tictactoe.common.PlayerEvent;
import de.geraldschenke.tictactoe.common.RemotePlayer;
import de.geraldschenke.tictactoe.server.GameThread;

public class RandomAI implements RemotePlayer {

	private GameThread gameThread;
	private int playerId = -1;

	public RandomAI(GameThread gameThread) {
		this.gameThread = gameThread;
	}

	@Override
	public void handleGameEvent(GameEvent gameEvent) {
		if (gameEvent.gameOver || gameEvent.currentPlayer != playerId) {
			Log.d("RandomAI", "got Event, but not my turn or Game Over");
			return;
		}
		int x;
		int y;
		do{
			x = (int) (Math.random()*gameEvent.board.length);
			y = (int) (Math.random()*gameEvent.board[0].length);
		}while(gameEvent.board[x][y] != null);
		
		PlayerEvent playerEvent = new PlayerEvent();
		playerEvent.type = PlayerEvent.Type.SET;
		playerEvent.x = x;
		playerEvent.y = y;
		gameThread.handlePlayerEvent(playerEvent);
	}

	@Override
	public void setPlayerId(int id) {
		playerId = id;
	}
}
